Suppose we have a neural network with one hidden layer and one output layer to predict the probability of a house being sold given (size, bedrooms, and bathrooms)
For simplicity, no bias term and no weights for the output layer, only average:
This is a composition of functions:
Computational Graph [Matrix Multiplication]
Matrix Multiplication: Training works on batches of data (e.g. 4 houses)
\[\begin{align*}
\begin{bmatrix}
x_1^{(1)} & x_2^{(1)} & x_3^{(1)} \\
x_1^{(2)} & x_2^{(2)} & x_3^{(2)} \\
x_1^{(3)} & x_2^{(3)} & x_3^{(3)} \\
x_1^{(4)} & x_2^{(4)} & x_3^{(4)}
\end{bmatrix} \times
\begin{bmatrix}
w_{1,1} & w_{1,2} & w_{1,3} \\
w_{2,1} & w_{2,2} & w_{2,3} \\
w_{3,1} & w_{3,2} & w_{3,3}
\end{bmatrix} =
\begin{bmatrix}
h_1^{(1)} & h_2^{(1)} & h_3^{(1)} \\
h_1^{(2)} & h_2^{(2)} & h_3^{(2)} \\
h_1^{(3)} & h_2^{(3)} & h_3^{(3)} \\
h_1^{(4)} & h_2^{(4)} & h_3^{(4)}
\end{bmatrix}
\end{align*}\] where \(x_i^{(j)}\) is feature \(i\) of house \(j\), \(w_{i,k}\) is the weight from input \(i\) to hidden node \(k\), and \(h_k^{(j)}\) is hidden node \(k\)’s value for house \(j\)
Computational Graph [Sigmoid]
Sigmoid: it is applied to each element of the hidden matrix
Every house now has an average probability of being sold predicted by three neurons.
Chain Rule of Neural Network
What is the derivative of \(O\) with respect to \(W\)?
\(\frac{\partial O}{\partial W} = \frac{\partial U}{\partial W} \odot \frac{\partial V}{\partial U} \odot \frac{\partial O}{\partial V}\) Should be same shape as W, this symbol \(\odot\) is element-wise multiplication
If each computation node in the graph has a known, easy-to-compute local derivative, we can compute the derivative of the entire graph with respect to the weights using the Chain Rule
TABLE OF CONTENTS
1. Neural Network as Computational Graph✓
2. Derivative of Matrix Multiplication●
3. Sigmoid on a Matrix (Element-wise)○
4. Row-wise average on a matrix○
5. Summary and practice○
Derivative of Functions with Multiple Outputs
For functions with multiple outputs, the derivative becomes a matrix called the Jacobian matrix:
The Jacobian matrix \(\frac{\partial O}{\partial W}\) will have dimensions \((6 \times 6)\) since \(O\) has 6 elements (3×2) and \(W\) has 6 elements (3×2). Each entry \((i,j)\) represents how the \(i\)-th element of \(O\) changes with respect to the \(j\)-th element of \(W\).
Compute \(U_{(1,3)}\), \(V_{(1,3)}\), and \(O_{(1,1)}\).
Write the chain rule for \(\displaystyle\frac{\partial O}{\partial W}\) and compute the derivative.
Numeric check: verify for one entry, e.g. \(w_{1,1}\), using \(\displaystyle\frac{\partial O}{\partial w_{1,1}} \approx \frac{O(w_{1,1}+\epsilon)-O(w_{1,1})}{\epsilon}\).
Numeric check for \(w_{1,1}\) with \(\epsilon = 10^{-4}\): \(\displaystyle\frac{\partial O}{\partial w_{1,1}} \approx \frac{O(w_{1,1}+\epsilon)-O(w_{1,1})}{\epsilon} \approx 0.0414\).
Thank You!
document.addEventListener("DOMContentLoaded", function() {
function handleQuizClick(e) {
// Find closest list item
const li = e.target.closest('li');
if (!li) return;
// Find the checkbox strictly within this LI (not nested deeper)
// We can check if the checkbox's closest LI is indeed this LI.
const checkbox = li.querySelector('input[type="checkbox"]');
if (!checkbox) return;
// Verify strict parent-child relationship to avoid triggering when clicking parent Question LI
if (checkbox.closest('li') !== li) return;
// We found a quiz item!
// Prevent default only if we are handling it.
// If the user clicked the checkbox directly, it might toggle. We want to force it to our logic.
e.stopPropagation();
// Determine correctness
// Quarto/Pandoc sets the 'checked' attribute for [x] items in the HTML source
// We rely on the attribute because the 'checked' property might have been toggled by the click before we got here if we didn't preventDefault fast enough.
// Actually, getting the initial state is safer.
const isCorrect = checkbox.hasAttribute('checked');
// Force the checkbox to match its "correct" state visually (optional, but good for consistency)
// checkbox.checked = isCorrect;
// We just want feedback colors.
// Reset classes
li.classList.remove('quiz-correct', 'quiz-incorrect');
// Apply feedback
if (isCorrect) {
li.classList.add('quiz-correct');
} else {
li.classList.add('quiz-incorrect');
}
}
function initQuiz() {
// Enable checkboxes and style them
const checkboxes = document.querySelectorAll(".reveal .slides li input[type='checkbox']");
checkboxes.forEach(cb => {
cb.disabled = false;
// Prevent default browser toggling logic on the input itself, let our handler manage expectation
cb.onclick = function(e) { e.preventDefault(); };
const li = cb.closest('li');
if (li) {
li.classList.add('quiz-option');
// Direct listener on LI is sometimes more reliable than delegation in complex frameworks
li.removeEventListener('click', handleQuizClick);
li.addEventListener('click', handleQuizClick);
}
});
}
// Initialize on Reveal ready
if (window.Reveal) {
if (window.Reveal.isReady()) {
initQuiz();
} else {
window.Reveal.on('ready', initQuiz);
}
window.Reveal.on('slidechanged', initQuiz);
// Also on fragment shown, as content might appear
window.Reveal.on('fragmentshown', initQuiz);
} else {
initQuiz();
}
});